package com.njrsrc.android.app;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.njrsrc.R;
import com.njrsrc.android.bean.PositionInfo;
import com.njrsrc.android.library.vpi.BaseSampleActivity;
import com.njrsrc.android.views.TitleBar;
import com.njrsrc.common.parser.json.CityInfo;
import com.njrsrc.common.parser.json.ProvinceInfo;

/**
 * 选择职位列表页面
 * 
 * @author Administrator
 * 
 */
public class SelectPositionActivity extends BaseSampleActivity implements OnClickListener {
	private ListView positionListView;
	private AreaAdapter adapter;
	private TitleBar mBar;
	public static List<ProvinceInfo> positionList;
	public static List<CityInfo> secondList;
	private TextView numberTextView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_area);
		positionList = AppApplication.positionList;
	}

	@Override
	protected void onResume() {
		initView();
		super.onResume();
	}

	private void initView() {
		mBar = (TitleBar) findViewById(R.id.activity_title_bar);
		mBar.setTitleText("选择职位");
		mBar.back.setOnClickListener(this);
		mBar.setRightBg(R.drawable.find_job_sub_title_tight);
		mBar.right.setOnClickListener(this);

		numberTextView = (TextView) findViewById(R.id.topic_number);

		positionListView = (ListView) findViewById(R.id.select_area_listView);
		adapter = new AreaAdapter(SelectPositionActivity.this, positionList);
		positionListView.setAdapter(adapter);
		try {
			updateTopic(null);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
	}

	private void updateTopic(RelativeLayout layout) throws IllegalArgumentException, IllegalAccessException,
			NoSuchFieldException {
		PositionInfo positionInfo = null;
		if(layout != null)
		{
			positionInfo = (PositionInfo) layout.getTag(R.id.position_info);
		}
		
		// 更新数字
		int number = AppApplication.choosePosition.size();
		if (number == 0) {
			numberTextView.setVisibility(View.GONE);
		} else {
			numberTextView.setVisibility(View.VISIBLE);
			numberTextView.setText(number + "/3");
				}
		if (positionInfo == null) {
			int size = AppApplication.choosePosition.size();
			if(size == 0)
			{
				findViewById(R.id.topic_tv01).setVisibility(View.GONE);
				findViewById(R.id.topic_tv02).setVisibility(View.GONE);
				findViewById(R.id.topic_tv03).setVisibility(View.GONE);
			}else
			{
				for (int i = 0; i < size; i++) {
					PositionInfo positionInfoT = AppApplication.choosePosition.get(i);
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (i + 1)).getInt(this)))
							.setText(positionInfoT.getName());
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (i + 1)).getInt(this)))
							.setVisibility(View.VISIBLE);
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (i + 1)).getInt(this)))
							.setBackgroundResource(R.drawable.chat_message_bg);
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (i + 1)).getInt(this)))
					.setTag(positionInfoT);
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (i + 1)).getInt(this)))
					.setOnClickListener(this);
				}
			}
			
			return;
		}
		if (!AppApplication.choosePosition.contains(positionInfo)) {
			if (AppApplication.choosePosition.size() == 3) {
				return;
			}
			AppApplication.choosePosition.add(positionInfo);
			for(int index = 0;index<3;index++)
			{
				TextView tv = ((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (index + 1)).getInt(this)));
				if(tv.getVisibility() == View.GONE)
				{
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (index + 1)).getInt(this)))
					.setText(positionInfo.getName());
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (index + 1)).getInt(this)))
							.setVisibility(View.VISIBLE);
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (index + 1)).getInt(this)))
							.setBackgroundResource(R.drawable.chat_message_bg);
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (index + 1)).getInt(this)))
					.setTag(layout);
					((TextView) findViewById(R.id.class.getDeclaredField("topic_tv0" + (index + 1)).getInt(this)))
					.setOnClickListener(this);
					break;
				}
			}
		} else {
			cancelTextViewSelected(positionInfo);
		}
		// 更新数字
		int number2 = AppApplication.choosePosition.size();
		if (number2 == 0) {
			numberTextView.setVisibility(View.GONE);
		} else {
			numberTextView.setVisibility(View.VISIBLE);
			numberTextView.setText(number2 + "/3");
		}
	}

	public class AreaAdapter extends BaseAdapter {
		private List<ProvinceInfo> list;
		private Context mContext;

		public AreaAdapter(Context mContext, List<ProvinceInfo> list) {
			this.mContext = mContext;
			this.list = list;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup arg2) {
			final Holder holder;
			if (convertView == null) {
				holder = new Holder();
				LayoutInflater inflater = LayoutInflater.from(mContext);
				convertView = inflater.inflate(R.layout.item_list_area, null);
				holder.layout = (RelativeLayout) convertView.findViewById(R.id.item_list_industry_layout);
				holder.name = (TextView) convertView.findViewById(R.id.item_list_area_tv);
				holder.arrow = (ImageView) convertView.findViewById(R.id.item_list_area_arrow);
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			List<CityInfo> cityList = list.get(position).getCityList();
			if (cityList != null && cityList.size() > 0) {
				// 说明有子节点
				holder.arrow.setVisibility(View.VISIBLE);
			} else {
				holder.arrow.setVisibility(View.GONE);
			}
			holder.name.setText(list.get(position).getName());

			if (positionList.get(position).getCode().equals(list.get(position).getCode())
					&& positionList.get(position).isSelected() && positionList.get(position).getCityList() == null) {
				holder.layout.setBackgroundResource(R.drawable.public_item_bg_press);
			} else {
				holder.layout.setBackgroundResource(R.drawable.public_item_selector);
			}
			OnClickListener listener = new OnClickListener() {

				@Override
				public void onClick(View v) {
					int number = AppApplication.choosePosition.size();
					List<CityInfo> cityList = positionList.get(position).getCityList();
					if (positionList.get(position).getCode().equals(list.get(position).getCode())
							&& !positionList.get(position).isSelected()) {
						if (number >= 3 && (cityList == null || cityList.size() == 0)) {
							return;
						}
						holder.layout.setBackgroundResource(R.drawable.public_item_bg_press);
						positionList.get(position).setSelected(true);
					} else {
						holder.layout.setBackgroundResource(R.drawable.public_item_selector);
						positionList.get(position).setSelected(false);
					}
					
					if (cityList != null && cityList.size() > 0) {
						// 说明有子节点
						SelectPositionActivity.secondList = cityList;
						Intent secondIntent = new Intent();
						secondIntent.setClass(SelectPositionActivity.this, SelectPositionSecondActivity.class);
						secondIntent.putExtra("title", positionList.get(position).getName());
						startActivityForResult(secondIntent, 0);
					} else {
						PositionInfo positionInfo = new PositionInfo(positionList.get(position).getName(), positionList
								.get(position).getCode());
						holder.layout.setTag(R.id.position_info,positionInfo);
						try {
							updateTopic(holder.layout);
						} catch (IllegalArgumentException e) {
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						} catch (NoSuchFieldException e) {
							e.printStackTrace();
						}
					}
				}
			};
			holder.layout.setOnClickListener(listener);
			return convertView;
		}
	}

	class Holder {
		RelativeLayout layout;
		TextView name;
		ImageView arrow;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		setResult(resultCode);
		// finish();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tb_ibtn_back:
			// AppApplication.clear();
			setResult(0);
			finish();
			break;
		case R.id.tb_ibtn_right:
			setResult(1);
			finish();
			break;
		case R.id.topic_tv01:
			PositionInfo positionInfo01 = null;
			if(v.getTag() != null && v.getTag() instanceof RelativeLayout)
			{
				RelativeLayout layout01 = (RelativeLayout) v.getTag();
				layout01.setBackgroundResource(R.drawable.public_item_selector);
				if(layout01 != null && layout01.getTag(R.id.position_info) != null && layout01.getTag(R.id.position_info) instanceof PositionInfo)
				{
					positionInfo01 = (PositionInfo) layout01.getTag(R.id.position_info);
				}
			}else if(v.getTag() != null && v.getTag() instanceof PositionInfo)
			{
				positionInfo01 = (PositionInfo) v.getTag();
			}
			if(positionInfo01 != null)
			{
				cancelTextViewSelected(positionInfo01);
			}
			break;
		case R.id.topic_tv02:
			PositionInfo positionInfo02 = null;
			if(v.getTag() != null && v.getTag() instanceof RelativeLayout)
			{
				RelativeLayout layout02 = (RelativeLayout) v.getTag();
				layout02.setBackgroundResource(R.drawable.public_item_selector);
				if(layout02 != null && layout02.getTag(R.id.position_info) != null && layout02.getTag(R.id.position_info) instanceof PositionInfo)
				{
					positionInfo02 = (PositionInfo) layout02.getTag(R.id.position_info);
				}
			}else if(v.getTag() != null && v.getTag() instanceof PositionInfo)
			{
				positionInfo02 = (PositionInfo) v.getTag();
			}
			if(positionInfo02 != null)
			{
				cancelTextViewSelected(positionInfo02);
			}
			break;
		case R.id.topic_tv03:
			PositionInfo positionInfo03 = null;
			if(v.getTag() != null && v.getTag() instanceof RelativeLayout)
			{
				RelativeLayout layout03 = (RelativeLayout) v.getTag();
				layout03.setBackgroundResource(R.drawable.public_item_selector);
				if(layout03 != null && layout03.getTag(R.id.position_info) != null && layout03.getTag(R.id.position_info) instanceof PositionInfo)
				{
					positionInfo03 = (PositionInfo) layout03.getTag(R.id.position_info);
				}
			}else if(v.getTag() != null && v.getTag() instanceof PositionInfo)
			{
				positionInfo03 = (PositionInfo) v.getTag();
			}
			if(positionInfo03 != null)
			{
				cancelTextViewSelected(positionInfo03);
			}
			break;
		default:
			break;
		}
	}
	
	private void cancelTextViewSelected(PositionInfo positionInfo)
	{
		int index = AppApplication.choosePosition.indexOf(positionInfo);
		TextView tv1 = (TextView) findViewById(R.id.topic_tv01);
		TextView tv2 = (TextView) findViewById(R.id.topic_tv02);
		TextView tv3 = (TextView) findViewById(R.id.topic_tv03);
		String title1Name = tv1.getText().toString();
		String title2Name = tv2.getText().toString();
		String title3Name = tv3.getText().toString();
		
		if(positionInfo.getName().equals(title1Name))
		{
			tv1.setText("");
			tv1.setVisibility(View.GONE);
			tv1.setBackgroundDrawable(null);
		}else if(positionInfo.getName().equals(title2Name))
		{
			tv2.setText("");
			tv2.setVisibility(View.GONE);
			tv2.setBackgroundDrawable(null);
		}else if(positionInfo.getName().equals(title3Name))
		{
			tv3.setText("");
			tv3.setVisibility(View.GONE);
			tv3.setBackgroundDrawable(null);
		}
		AppApplication.choosePosition.remove(index);
		
		// 更新数字
		int number = AppApplication.choosePosition.size();
		if (number == 0) {
			numberTextView.setVisibility(View.GONE);
		} else {
			numberTextView.setVisibility(View.VISIBLE);
			numberTextView.setText(number + "/3");
				}
		
		for(int i = 0;i < positionList.size();i++)
		{
			ProvinceInfo p = positionList.get(i);
			if(positionInfo.getCode().equals(p.getCode()))
			{
				p.setSelected(false);
			}
			List<CityInfo> cityList = p.getCityList();
			if(cityList != null && cityList.size() != 0)
			{
				for(int j = 0;j < cityList.size();j++)
				{
					CityInfo c = cityList.get(j);
					if(positionInfo.getCode().equals(c.getCode()))
					{
						c.setSelected(false);
					}
				}
			}
		}
	}
}